<?php

/**
 * This is the model class for table "player".
 *
 * The followings are the available columns in table 'player':
 * @property integer $id
 * @property integer $federation_id
 * @property integer $category_id
 * @property string $name
 * @property string $registry_date
 * @property string $natural_country
 * @property string $natural_state
 * @property string $natural_city
 * @property string $nickname
 * @property string $birthy_date
 * @property string $gender
 * @property string $father_name
 * @property string $mother_name
 * @property string $email
 * @property string $address
 * @property string $neightborhood
 * @property string $state
 * @property string $city
 * @property integer $cert_birthy
 * @property string $rg
 * @property string $emissor
 * @property string $doc_validate
 * @property string $cpf
 * @property string $ctps
 * @property string $cnh
 *
 * The followings are the available model relations:
 * @property Category $category
 */
class Player extends CActiveRecord {
	public static $photo_name = 'photo_player_';
	
	/**
	 *
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'player';
	}
	
	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave() {
		parent::afterSave ();
	}
	
	/**
	 *
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array (
				array (
						'name, federation_id, category_id, registry_date, natural_country, natural_state, natural_city, nickname, birthy_date, mother_name, rg, emissor, ctps, cnh',
						'required' 
				),
				array (
						'category_id, cert_birthy',
						'numerical',
						'integerOnly' => true 
				),
				array (
						'name, nickname, father_name, mother_name, email, address',
						'length',
						'max' => 250 
				),
				array (
						'natural_country, natural_state, natural_city, gender, neightborhood, state, city, emissor, cnh',
						'length',
						'max' => 45 
				),
				array (
						'rg, ctps, federation_id',
						'length',
						'max' => 20 
				),
				array (
						'rg, ctps, email, cpf, federation_id',
						'unique' 
				),
				array (
						'cpf',
						'length',
						'max' => 13 
				),
				array (
						'doc_validate',
						'safe' 
				),
				// The following rule is used by search().
				// @todo Please remove those attributes that should not be searched.
				array (
						'id, federation_id, category_id, name, registry_date, natural_country, natural_state, natural_city, nickname, birthy_date, gender, father_name, mother_name, email, address, neightborhood, state, city, cert_birthy, rg, emissor, doc_validate, cpf, ctps, cnh',
						'safe',
						'on' => 'search' 
				) 
		);
	}
	
	/**
	 *
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array (
				'category' => array (
						self::BELONGS_TO,
						'Category',
						'category_id' 
				) 
		);
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
				'id' => 'ID',
				'federation_id' => 'Id',
				'category_id' => 'Clube',
				'name' => 'Nome',
				'registry_date' => 'Data de Registro',
				'natural_country' => 'Nacionalidade',
				'natural_state' => 'Estado Natal',
				'natural_city' => 'Cidade Natal',
				'nickname' => 'Apelido',
				'birthy_date' => 'Data de Nascimento',
				'gender' => 'Sexo',
				'father_name' => 'Nome do Pai',
				'mother_name' => 'Nome da Mãe',
				'email' => 'Email',
				'address' => 'Endereço',
				'neightborhood' => 'Bairro',
				'state' => 'Estado',
				'city' => 'Cidade',
				'cert_birthy' => 'Cert. de Nascimento',
				'rg' => 'Rg',
				'emissor' => 'Emissor',
				'doc_validate' => 'Validade',
				'cpf' => 'Cpf',
				'ctps' => 'Ctps',
				'cnh' => 'Cnh' 
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 *         based on the search/filter conditions.
	 */
	public function search() {
		// @todo Please modify the following code to remove attributes that should not be searched.
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'id', $this->id );
		$criteria->compare ( 'federation_id', $this->federation_id );
		$criteria->compare ( 'category_id', $this->category_id );
		$criteria->compare ( 'name', $this->name, true );
		$criteria->compare ( 'registry_date', $this->registry_date, true );
		$criteria->compare ( 'natural_country', $this->natural_country, true );
		$criteria->compare ( 'natural_state', $this->natural_state, true );
		$criteria->compare ( 'natural_city', $this->natural_city, true );
		$criteria->compare ( 'nickname', $this->nickname, true );
		$criteria->compare ( 'birthy_date', $this->birthy_date, true );
		$criteria->compare ( 'gender', $this->gender, true );
		$criteria->compare ( 'father_name', $this->father_name, true );
		$criteria->compare ( 'mother_name', $this->mother_name, true );
		$criteria->compare ( 'email', $this->email, true );
		$criteria->compare ( 'address', $this->address, true );
		$criteria->compare ( 'neightborhood', $this->neightborhood, true );
		$criteria->compare ( 'state', $this->state, true );
		$criteria->compare ( 'city', $this->city, true );
		$criteria->compare ( 'cert_birthy', $this->cert_birthy );
		$criteria->compare ( 'rg', $this->rg, true );
		$criteria->compare ( 'emissor', $this->emissor, true );
		$criteria->compare ( 'doc_validate', $this->doc_validate, true );
		$criteria->compare ( 'cpf', $this->cpf, true );
		$criteria->compare ( 'ctps', $this->ctps, true );
		$criteria->compare ( 'cnh', $this->cnh, true );
		
		$criteria->condition = 't.status = :status OR t.status = :status2';
		$criteria->params = array (
				':status' => 'ATIVO',
				':status2' => 'EM APROVAÇÃO' 
		);
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria 
		) );
	}
	public function customSearch($condition) {
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'id', $this->id );
		$criteria->compare ( 'federation_id', $this->federation_id );
		$criteria->compare ( 'category_id', $this->category_id );
		$criteria->compare ( 'name', $this->name, true );
		$criteria->compare ( 'registry_date', $this->registry_date, true );
		$criteria->compare ( 'natural_country', $this->natural_country, true );
		$criteria->compare ( 'natural_state', $this->natural_state, true );
		$criteria->compare ( 'natural_city', $this->natural_city, true );
		$criteria->compare ( 'nickname', $this->nickname, true );
		$criteria->compare ( 'birthy_date', $this->birthy_date, true );
		$criteria->compare ( 'gender', $this->gender, true );
		
		if (Yii::app ()->user->name == 'admin') {
			$criteria->condition = 't.name LIKE :query OR t.nickname LIKE :query OR t.email LIKE :query';
			$criteria->params = array (
					':query' => $condition 
			);
		} else {
			
			$criteria->condition = ' (t.name LIKE :query OR t.nickname LIKE :query OR t.email LIKE :query) AND (t.status = :status)';
			$criteria->params = array (
					':status' => 'ATIVO',
					':query' => $condition 
			);
		}
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria 
		) );
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * 
	 * @param string $className
	 *        	active record class name.
	 * @return Player the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	public function getBirthDate() {
		return Yii::app ()->dateFormatter->format ( Utils::$dateTimeOutcomeFormat, CDateTimeParser::parse ( $this->birthy_date, Utils::$dateTimeIncomeFormat ) );
	}
	public function setBirthDate($datetime) {
		$this->birthy_date = Utils::changeDate ( $datetime );
	}
	public function getRegistryDate() {
		return Yii::app ()->dateFormatter->format ( Utils::$dateTimeOutcomeFormat, CDateTimeParser::parse ( $this->registry_date, Utils::$dateTimeIncomeFormat ) );
	}
	public function setRegistryDate($datetime) {
		$this->registry_date = Yii::app ()->dateFormatter->format ( Utils::$dateTimeIncomeFormat, CDateTimeParser::parse ( $datetime, Utils::$dateTimeOutcomeFormat ) );
	}
	public function getDocValidate() {
		return Yii::app ()->dateFormatter->format ( Utils::$dateTimeOutcomeFormat, CDateTimeParser::parse ( $this->doc_validate, Utils::$dateTimeIncomeFormat ) );
	}
	public function setDocValidate($datetime) {
		$this->doc_validate = Yii::app ()->dateFormatter->format ( Utils::$dateTimeIncomeFormat, CDateTimeParser::parse ( $datetime, Utils::$dateTimeOutcomeFormat ) );
	}
}
